package torres.view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.swing.JPanel;

import torres.entity.Entity;
import torres.item.EquippableItem;
import torres.item.EquippedInventory;
import torres.item.Inventory;
import torres.item.TakeableItem;
import torres.map.World;

@SuppressWarnings("serial")
public class InventoryView extends JPanel{

	int screenWidth ,x;
	int screenHeight, y;
	Font body;
	Font headers;
	Entity player;
	HashMap<Integer,Dimension> inventoryTable;
	HashMap<Integer,Dimension> equippedTable;
	String extension = new String("_small.png");
	
	public InventoryView() {

		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		screenWidth = screenSize.width;
		screenHeight = screenSize.height;
		
		x = 2*screenWidth/3;
		y = 2*screenHeight/3;
		
		body = new Font("Helvetica",Font.BOLD, 14);
		headers = new Font("Helvetica",Font.ITALIC, 18);
		
		setIgnoreRepaint(true);
		setPreferredSize(new Dimension(x,y));
		setLocation(screenWidth-x/2, screenHeight-y/2);
		setLayout(null);
		setVisible(true);
		setFocusable(true);
		setOpaque(true);
		
		player = World.getInstance().getPlayer();

	}
	
	public synchronized void paintComponent(Graphics g) {
		super.paintComponent(g);
		player = World.getInstance().getPlayer();
		Graphics2D g2 = (Graphics2D)g;
		
		g.setColor(Color.black);
		drawWindow(g);

		int yOffset = 35;
		int xOffset = 20;
		
		g.setColor(Color.white);
		g.setFont(headers);
		g.drawString("Stats",screenWidth/2-x/2 + xOffset,screenHeight/2-y/2 + yOffset );
		g.drawString("Equipped", screenWidth/2 - x/2 + xOffset, screenHeight/2 + yOffset);
		g.drawString("Items", screenWidth/2 + xOffset, screenHeight/2 -y/2 + yOffset);
		
		drawPlayerImage(g2);
		g.setFont(body);
		g.setColor(Color.orange);
		drawStats(g2);
		drawEquippedItems(g2);
		drawItems(g2);

		//System.out.println("Finished painting");
		
	}
	
	public void drawItems(Graphics2D g2) {
		player = World.getInstance().getPlayer();
		Inventory inventory = player.getInventory();
		List<TakeableItem> itemList = inventory.getItemList();
		
		int itemXSize;
		int itemYSize;
		
		if (!itemList.isEmpty()) {
			torres.item.Item itemSample = itemList.get(0);
			itemXSize = ImageLoader.loadImage(itemSample.toString() + extension).getWidth() + 30;
			itemYSize = ImageLoader.loadImage(itemSample.toString() + extension).getHeight() + 30;
		}
		else {
			itemXSize = 40 + 30;
			itemYSize = 40 + 30;
		}
		
		int xOffset = 25;
		int yOffset = 35;
		int ySpace = 20;
		
		g2.setColor(Color.darkGray);
		
		for (int i=0; i < inventory.getMaxSize()/4 ; i++) {
			
			g2.draw3DRect(screenWidth/2 + xOffset,
					screenHeight/2 - y/2 + yOffset + ySpace + i*itemXSize,
					itemXSize + 2,
					itemYSize + 2,
					true);
			
			g2.draw3DRect(screenWidth/2 + x/8 + xOffset,
					screenHeight/2 - y/2 + yOffset + ySpace + i*itemXSize,
					itemXSize + 2,
					itemYSize + 2,
					true);
			
			g2.draw3DRect(screenWidth/2 + x/4 + xOffset,
					screenHeight/2 - y/2 + yOffset + ySpace + i*itemXSize,
					itemXSize + 2,
					itemYSize + 2,
					true);
			
			g2.draw3DRect(screenWidth/2 + 3*x/8 + xOffset,
					screenHeight/2 - y/2 + yOffset + ySpace + i*itemXSize,
					itemXSize + 2,
					itemYSize + 2,
					true);
			
			ySpace += 15;
			
		}
		int i = 0;
		int index = 1;
		int col = 0;
		ySpace = 20;
		inventoryTable = new HashMap<Integer,Dimension>();
		
		for (TakeableItem item: itemList) {
			
			int xt;
			int yt;
			
			itemXSize = ImageLoader.loadImage(item.toString() + extension).getWidth() + 30;
			itemYSize = ImageLoader.loadImage(item.toString() + extension).getHeight() + 30;
			
			if(col == 0 || col == 4 || col == 8 || col == 12 || col == 16 || col == 20) {
				xt = screenWidth/2 + xOffset + 2;
				yt = screenHeight/2 - y/2 + yOffset + ySpace + i*itemXSize + 2;
				g2.drawImage(ImageLoader.loadImage(item.toString() + extension),
					xt,yt,itemXSize,itemYSize,null);
				col++;
				inventoryTable.put(index++, new Dimension(xt,yt));
			}
			else if(col == 1 || col == 5 || col == 9 || col == 13 || col == 17 || col == 21) {
				xt = screenWidth/2 + x/8 + xOffset;
				yt = screenHeight/2 - y/2 + yOffset + ySpace + i*itemXSize;
				g2.drawImage(ImageLoader.loadImage(item.toString() + extension),
						xt,yt,itemXSize + 2,itemYSize + 2,null);
				col++;
				inventoryTable.put(index++, new Dimension(xt,yt));
			}
			else if(col == 2 || col == 6 || col == 10 || col == 14 || col == 18 || col == 22) {
				xt = screenWidth/2 + x/4 + xOffset;
				yt = screenHeight/2 - y/2 + yOffset + ySpace + i*itemXSize;
				g2.drawImage(ImageLoader.loadImage(item.toString() + extension),
						xt,yt,itemXSize + 2,itemYSize + 2,null);
				col++;
				inventoryTable.put(index++,new Dimension(xt,yt));
			}
			else if(col == 3 || col == 7 || col == 11 || col == 15 || col == 19 || col == 23) {
				xt = screenWidth/2 + 3*x/8 + xOffset;
				yt = screenHeight/2 - y/2 + yOffset + ySpace + i*itemXSize;
				g2.drawImage(ImageLoader.loadImage(item.toString() + extension),
						xt,yt,itemXSize + 2,itemYSize + 2,null);
				inventoryTable.put(index++,new Dimension(xt,yt));
				col++;
				i++;
				ySpace += 15;
			}
		}
		
	}
	
	public void drawWindow(Graphics g) {
		ImageLoader loader = new ImageLoader("texture.jpg");
		BufferedImage img = loader.getImage();
		
		g.drawImage(img, screenWidth/2-x/2,screenHeight/2-y/2,x,y,null);
		
		/*
		g.draw3DRect(screenWidth/2-x/2,screenHeight/2-y/2, x, y, true);
		g.setColor(Color.darkGray);
		g.fill3DRect(screenWidth/2-x/2,
				screenHeight/2-y/2,
				x,y,
				true);
		*/
	}
	public void drawEquippedItems(Graphics2D g2) {
		int sx = 80;
		int sy= 80;
		int delta = 4;
		
		player = World.getInstance().getPlayer();
		EquippedInventory equippedInventory = player.getEquippedInventory();
		ArrayList<EquippableItem> equippedList = equippedInventory.getItemList();
		equippedTable = new HashMap<Integer,Dimension>();
		
		g2.setColor(Color.darkGray);
		g2.draw3DRect(screenWidth/2-x/2 + x/16    , screenHeight/2 + y/8 - 30, sx, sy,true);
		g2.draw3DRect(screenWidth/2-x/2 + x/16 + 2, screenHeight/2 + y/8 - 30 + 2, sx-delta, sy-delta,false);
		g2.draw3DRect(screenWidth/2-x/2 + x/16    , screenHeight/2 + y/4 - 30 + 60, sx, sy,true);
		g2.draw3DRect(screenWidth/2-x/2 + x/16 + 2, screenHeight/2 + y/4 - 30 + 62, sx-delta, sy-delta ,false);
		g2.draw3DRect(screenWidth/2-x/2 + x/16 + x/16 + sx    , screenHeight/2 + y/8 - 30, sx, sy,true);
		g2.draw3DRect(screenWidth/2-x/2 + x/16 + x/16 + sx + 2, screenHeight/2 + y/8 - 30 + 2, sx-delta, sy-delta,false);
		g2.draw3DRect(screenWidth/2-x/2 + x/16 + x/16 + sx    , screenHeight/2 + y/4 - 30 + 60, sx, sy,true);
		g2.draw3DRect(screenWidth/2-x/2 + x/16 + x/16 + sx + 2, screenHeight/2 + y/4 - 30 + 62, sx-delta, sy-delta,false);
		g2.draw3DRect(screenWidth/2-x/2 + x/16 + 2*(x/16 + sx)    , screenHeight/2 + y/8- 30, sx, sy,true);
		g2.draw3DRect(screenWidth/2-x/2 + x/16 + 2*(x/16 + sx) + 2, screenHeight/2 + y/8- 30 + 2, sx-delta, sy-delta,false);
		g2.draw3DRect(screenWidth/2-x/2 + x/16 + 2*(x/16 + sx)    , screenHeight/2 + y/4- 30 + 60, sx, sy,true);
		g2.draw3DRect(screenWidth/2-x/2 + x/16 + 2*(x/16 + sx) + 2, screenHeight/2 + y/4- 30 + 62, sx-delta, sy-delta,false);
		
		int i=0;
		int index =1;
		int row =0;
		for (EquippableItem item : equippedList) {
			BufferedImage image = ImageLoader.loadImage(item.toString() + ".png");
			if(row++ < 3) {
				int xt = screenWidth/2 - x/2 + x/16 + i*(x/16 + sx) + 4;
				int yt = screenHeight/2 + y/8 - 26;
				g2.drawImage(image,	xt,yt,72, 72, null);
				equippedTable.put(index++, new Dimension(xt,yt));
				i++;
			}
			else {
				int xt = screenWidth/2 - x/2 + x/16 + i*(x/16 + sx) + 4;
				int yt = screenHeight/2 + y/4 - 30 + 64;
				g2.drawImage(image,	xt,yt,	72, 72, null);
				equippedTable.put(index++,new Dimension(xt,yt));
				i++;
			}
			if(i >= 3) {
				i=0;
			}
		}
	}

	public void drawPlayerImage(Graphics2D g2) {
		
		player = World.getInstance().getPlayer();
		g2.drawImage(player.getImage(),
				screenWidth/2-x/4-player.getImage().getWidth()/2,
				screenHeight/2 -y/4 -player.getImage().getHeight()/2,
				null);
	}
	
	public void drawStats(Graphics2D g2) {

		player = World.getInstance().getPlayer();
		String name = new String(player.getDisplayName());
		String occupation = new String(player.getOccupation().toString());
		String agility = new String(Integer.toString(player.getAgility()));
		String intellect = new String(Integer.toString(player.getIntellect()));
		String strength = new String(Integer.toString(player.getAgility()));
		String hardiness = new String(Integer.toString(player.getHardiness()));
		String observation = new String(Integer.toString(player.getObservation()));
		String offensive = new String(Integer.toString(player.getOffensiveRating()));
		String defensive = new String(Integer.toString(player.getDefensiveRating()));
		String armor = new String(Integer.toString(player.getArmorRating()));
		String movement = new String(Integer.toString(player.getMovement()));
		
		int yOffset = 50;
		g2.drawString("Name: " + name,screenWidth/2-x/2 + 42,screenHeight/2-y/2 +yOffset);
		g2.drawString("Occupation: " + occupation,screenWidth/2-x/2 + 42,screenHeight/2-y/2 +yOffset+20);
		g2.drawString("Agility: " + agility,screenWidth/2-x/2 + 42,screenHeight/2-y/2 +yOffset+40);
		g2.drawString("Intellect: " + intellect,screenWidth/2-x/2 + 42,screenHeight/2-y/2 +yOffset+60);
		g2.drawString("Strength: " + strength,screenWidth/2-x/2 + 42,screenHeight/2-y/2 +yOffset+80);
		g2.drawString("Hardiness: " + hardiness,screenWidth/2-x/2 + 42,screenHeight/2-y/2 +yOffset+100);
		g2.drawString("Observation: " + observation,screenWidth/2-x/2 + 42,screenHeight/2-y/2 +yOffset+120);
		g2.drawString("Defensive Rating: " + defensive,screenWidth/2-x/2 + 42,screenHeight/2-y/2 +yOffset+140);
		g2.drawString("Offensive Rating: " + offensive,screenWidth/2-x/2 + 42,screenHeight/2-y/2 +yOffset+160);
		g2.drawString("Armor Rating: " + armor,screenWidth/2-x/2 + 42, screenHeight/2-y/2 +yOffset+180);
		g2.drawString("Movement: " + movement,screenWidth/2-x/2 + 42,screenHeight/2-y/2 +yOffset+200);
	}
	public void update(Graphics g) {
		paintComponent(g);
	}
	
	public int getScreenWidth() {
		return screenWidth;
	}

	public int getX() {
		return x;
	}

	public int getScreenHeight() {
		return screenHeight;
	}

	public int getY() {
		return y;
	}
	public HashMap<Integer,Dimension> getInventoryTable() {
		return inventoryTable;
	}
	public HashMap<Integer,Dimension> getEquippedTable() {
		return equippedTable;
	}
}
